<?php

namespace app\factory\logic;

use app\common\exception\LogicException;
use app\model\FourteenFeedPlan;
use think\facade\Db;

class FourteenFeedPlanLogic
{
    protected FourteenFeedPlan $model;
    protected int $factory_id;

    public function __construct()
    {
        $this->model = new FourteenFeedPlan();
        $this->factory_id = request()->factory_info['factory_id'];
    }

    /**
     * @Description 新增/编辑
     * @author: zj
     * @param int $Id
     * @param array $param
     * @time 2024/03/23 12:15:22
     * @return true
     */
    public function addEdit(int $Id, array $param)
    {
        Db::startTrans();
        try {
            $exist = $this->model->checkOutIdIsExist($this->factory_id,$Id);
            if ($exist) {
                $rule = [
                    "Name" => "max:50",
                    "DayId" => "max:1500",
                    "isLoop" => "integer|in:0,1",
                ];
                validate($rule)->check($param);

                $updateData = [];
                $updateData['update_time'] = request()->time();
                if ($param['Name']) {
                    $plan = $this->model->checkNameIsExist($this->factory_id,$param['Name']);
                    if ($plan && $plan != $exist)  {
                        throw new LogicException('计划名已存在');
                    }
                    $updateData['name'] = $param['Name'];
                }
                if ($param['DayId']) {
                    $updateData['day_id'] = $param['DayId'];
                }
                if ($param['FeedWeight']) {
                    $updateData['feed_weight'] = $param['FeedWeight'];
                }
                if (isset($param['isLoop'])) {
                    $updateData['is_loop'] = $param['isLoop'];
                }
                if (isset($param['Remark'])) {
                    $updateData['remark'] = $param['Remark'];
                }
                $updateData['update_record_time'] = $param['RecordTime'];

                $this->model->where('out_id',$Id)
                    ->where('delete_time',$this->model::IS_DELETE_NO)
                    ->update($updateData);
            } else {
                $rule = [
                    "Name" => "require|require|max:50",
                    "DayId" => "require|max:1500",
                    "isLoop" => "require|integer|in:0,1",
                ];
                validate($rule)->check($param);

                $this->model->insert([
                    'out_id' => $Id,
                    'factory_id' => $this->factory_id,
                    'name' => $param['Name'],
                    'day_id' => $param['DayId'],
                    'is_loop' => $param['isLoop'],
                    'remark' => $param['Remark'],
                    'record_time' => $param['RecordTime'],
                    'update_record_time' => $param['RecordTime'],
                    'create_time' => request()->time(),
                    'update_time' => request()->time()
                ]);
            }

            Db::commit();
        }catch (\Exception $exception) {
            Db::rollback();
            throw new LogicException($exception->getMessage());
        }

        return true;
    }
}